package cn.vconinfo.base.soap;

import org.apache.commons.io.IOUtils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

public class SoapUtil {

    public static String getSoapResult(String xml, String url, String SOAPAction) throws IOException {
        String wsdl = url;
        int timeout = 40000;//增加给40s的请求时间
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(wsdl);
        // 设置连接超时
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        // 设置读取时间超时
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        // 然后把Soap请求数据添加到PostMethod中
        RequestEntity requestEntity = new StringRequestEntity(xml, "text/xml", "UTF-8");
        //设置请求头部，否则可能会报 “no SOAPAction header” 的错误
        postMethod.setRequestHeader("SOAPAction",SOAPAction);
        //        postMethod.setRequestHeader("Content-Length", String.valueOf(xml.length()));
        // 设置请求体
        postMethod.setRequestEntity(requestEntity);
        int status = client.executeMethod(postMethod);
        // 获取响应体输入流
        InputStream is = postMethod.getResponseBodyAsStream();
        // 获取请求结果字符串
        String result = IOUtils.toString(is);
        // 替换符号
        result = result.replaceAll("&lt;", "<");
        result = result.replaceAll("&gt;", ">");
        return result;
    }

    public static String getAdkSoapResult(String xml, String url, String SOAPAction) throws IOException {
        String wsdl = url;
        int timeout = 40000;//增加给40s的请求时间
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(wsdl);
        // 设置连接超时
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        // 设置读取时间超时
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        // 然后把Soap请求数据添加到PostMethod中
        RequestEntity requestEntity = new StringRequestEntity(xml, "text/xml", "utf-8");
        //设置请求头部，否则可能会报 “no SOAPAction header” 的错误
        postMethod.setRequestHeader("SOAPAction",SOAPAction);
        //        postMethod.setRequestContentLength(xml.length());
        //        postMethod.setRequestHeader("Content-Type","text/xml; charset=utf-8");
        //        postMethod.setRequestHeader("Content-Length",String.valueOf(xml.length()));
        //        postMethod.setRequestHeader("Content-Length", String.valueOf(xml.length()));
        //        postMethod.setRequestContentLength(xml.length());
        // 设置请求体
        postMethod.setRequestEntity(requestEntity);
        int status = client.executeMethod(postMethod);
        // 获取响应体输入流
        InputStream is = postMethod.getResponseBodyAsStream();
        // 获取请求结果字符串
        String result = IOUtils.toString(is);

        //        URL requestUrl = new URL(wsdl);
        //        HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
        //
        //        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        //        conn.setRequestProperty("Content-Length", String.valueOf(xml.length()));
        //        conn.setRequestMethod("POST");
        //        conn.setUseCaches(false);
        //        conn.setDoInput(true);
        //        conn.setDoOutput(true);
        //        conn.setConnectTimeout(timeout);
        //        conn.setReadTimeout(timeout);
        //        StringBuffer sb = new StringBuffer("");
        //        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
        //        dos.write(sb.toString().getBytes("utf-8"));
        //        dos.flush();

        //        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        //        String line = null;
        //        StringBuffer strBuf = new StringBuffer();
        //        while ((line = reader.readLine()) != null) {
        //            strBuf.append(line);
        //        }
        //        dos.close();
        //        reader.close();
        //
        //        String result = new String(strBuf);
        // 替换符号
        result = result.replaceAll("&lt;", "<");
        result = result.replaceAll("&gt;", ">");
        return result;
    }

}
